#include <string.h>
#include <iostream>
#include <vector>
#include <chrono>

int main()
{
	{
		// chrono recorde time
		std::cout << "insert:\t\t";
		auto ts = std::chrono::system_clock::now();

		for (int k = 0; k < 100000; k++)
		{
			std::vector<int> v1;
			std::vector<int> v2;
			for (int i = 0; i < 128; i++)
			{
				v1.push_back(i);
				v2.push_back(10000 + i);
			}

			// 在v1的末尾插入v2的所有元素
			v1.insert(v1.end(), v2.begin(), v2.end());
		}
		auto te = std::chrono::system_clock::now();
		std::cout << (double)(te - ts).count() / 1000000 << "ms" << std::endl;
	}

	{
		// chrono recorde time
		std::cout << "push_back1:\t";
		auto ts = std::chrono::system_clock::now();

		for (int k = 0; k < 100000; k++)
		{
			std::vector<int> v1;
			std::vector<int> v2;
			for (int i = 0; i < 128; i++)
			{
				v1.push_back(i);
				v2.push_back(10000 + i);
			}

			// 遍历v2，将每个元素添加到v1的末尾
			for (auto it = v2.begin(); it != v2.end(); ++it)
			{
				v1.push_back(*it);
			}
		}
		auto te = std::chrono::system_clock::now();
		std::cout << (double)(te - ts).count() / 1000000 << "ms" << std::endl;
	}

	{
		// chrono recorde time
		std::cout << "push_back2:\t";
		auto ts = std::chrono::system_clock::now();

		for (int k = 0; k < 100000; k++)
		{
			std::vector<int> v1;
			std::vector<int> v2;
			for (int i = 0; i < 128; i++)
			{
				v1.push_back(i);
				v2.push_back(10000 + i);
			}

			// 预分配足够的内存空间
			v1.reserve(v1.size() + v2.size());

			// 使用push_back拼接
			for (auto it = v2.begin(); it != v2.end(); ++it)
			{
				v1.push_back(*it);
			}
		}
		auto te = std::chrono::system_clock::now();
		std::cout << (double)(te - ts).count() / 1000000 << "ms" << std::endl;
	}

	{
		// chrono recorde time
		std::cout << "emplace_back:\t";
		auto ts = std::chrono::system_clock::now();

		for (int k = 0; k < 100000; k++)
		{
			std::vector<int> v1;
			std::vector<int> v2;
			for (int i = 0; i < 128; i++)
			{
				v1.push_back(i);
				v2.push_back(10000 + i);
			}

			v1.reserve(v1.size() + v2.size());

			// 使用emplace_back拼接
			for (auto it = v2.begin(); it != v2.end(); ++it)
			{
				v1.emplace_back(*it);
			}
		}
		auto te = std::chrono::system_clock::now();
		std::cout << (double)(te - ts).count() / 1000000 << "ms" << std::endl;
	}

	{
		// chrono recorde time
		std::cout << "memcpy1:\t";
		auto ts = std::chrono::system_clock::now();

		for (int k = 0; k < 100000; k++)
		{
			std::vector<int> v1;
			std::vector<int> v2;
			for (int i = 0; i < 128; i++)
			{
				v1.push_back(i);
				v2.push_back(10000 + i);
			}

			int n1 = (v1.size() * sizeof(int));
			int n2 = (v2.size() * sizeof(int));
			v1.resize(v1.size() + v2.size());
			// v1.reserve(v1.size() + v2.size());

			char *p1 = (char *)v1.data();
			char *p2 = (char *)v2.data();
			// 使用memcpy拼接
			memcpy((p1 + n1), p2, n2);
			//for (int num : v1)
			//{
			//	std::cout << num << " ";
			//}
		}
		auto te = std::chrono::system_clock::now();
		std::cout << (double)(te - ts).count() / 1000000 << "ms" << std::endl;
	}

	{
		// chrono recorde time
		std::cout << "memcpy2:\t";
		auto ts = std::chrono::system_clock::now();

		for (int k = 0; k < 100000; k++)
		{
			std::vector<int> v1;
			std::vector<int> v2;
			int N = 128;
			v1.reserve(N << 1);
			v2.reserve(N);
			for (int i = 0; i < N; i++)
			{
				v1.push_back(i);
				v2.push_back(10000 + i);
			}

			int n1 = (v1.size() * sizeof(int));
			int n2 = (v2.size() * sizeof(int));
			v1.resize(v1.size() + v2.size());
			// v1.reserve(v1.size() + v2.size());

			char *p1 = (char *)v1.data();
			char *p2 = (char *)v2.data();
			// 使用memcpy拼接
			memcpy((p1 + n1), p2, n2);
			//for (int num : v1)
			//{
			//	std::cout << num << " ";
			//}
		}
		auto te = std::chrono::system_clock::now();
		std::cout << (double)(te - ts).count() / 1000000 << "ms" << std::endl;
	}

	{
		// chrono recorde time
		std::cout << "memcpy3:\t";
		auto ts = std::chrono::system_clock::now();

		for (int k = 0; k < 100000; k++)
		{
			int N = 128;
			std::vector<int> v1(N << 1);
			std::vector<int> v2(N);
			for (int i = 0; i < N; i++)
			{
				v1[i] = i;
				v2[i] = 10000 + i;
			}

			int n1 = (N * sizeof(int));
			int n2 = (v2.size() * sizeof(int));

			char *p1 = (char *)v1.data();
			char *p2 = (char *)v2.data();
			// 使用memcpy拼接
			memcpy((p1 + n1), p2, n2);
			//for (int num : v1)
			//{
			//	std::cout << num << " ";
			//}
		}
		auto te = std::chrono::system_clock::now();
		std::cout << (double)(te - ts).count() / 1000000 << "ms" << std::endl;
	}

	return 0;
}

// xtwh@u2004:~/progs/MergeVector/build$ /home/xtwh/progs/MergeVector/build/MergeVector
// insert:         470.087ms
// push_back1:     641.589ms
// push_back2:     652.067ms
// emplace_back:   630.635ms
// memcpy1:        450.362ms
// memcpy2:        297.771ms
// memcpy3:        129.34ms
// xtwh@u2004:~/progs/MergeVector/build$ /home/xtwh/progs/MergeVector/build/MergeVector
// insert:         473.376ms
// push_back1:     624.896ms
// push_back2:     623.548ms
// emplace_back:   611.882ms
// memcpy1:        430.564ms
// memcpy2:        290.165ms
// memcpy3:        128.776ms
// xtwh@u2004:~/progs/MergeVector/build$ /home/xtwh/progs/MergeVector/build/MergeVector
// insert:         439.023ms
// push_back1:     633.801ms
// push_back2:     626.997ms
// emplace_back:   618.82ms
// memcpy1:        435.39ms
// memcpy2:        308.464ms
// memcpy3:        130.721ms
